package com.example.test

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import com.example.test.databinding.FragmentBlankBinding


class BlankFragment : Fragment() {
    private val viewModel: ViewModelWithLiveData by activityViewModels()

    //  private  var viewModle:ViewModleWithLiveData ?=null
    private lateinit var binding: FragmentBlankBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBlankBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //val viewModle =   ViewModelProvider.NewInstanceFactory().create(ViewModleWithLiveData::class.java)

        binding.age.text = viewModel.getData()!!.value?.age.toString()
        viewModel.getData()!!.observe(viewLifecycleOwner,
            Observer { t -> binding.age.text = t?.age.toString() })

        setOnClick()
    }

    private fun setOnClick() {
        binding.add.setOnClickListener {
            viewModel?.addAge()
        }
        binding.delete.setOnClickListener {
            viewModel?.subAge()
        }
    }


}